package com.wwwarehouse.resource_center.fragment.rules;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.adapter.rules.SelectObjAdapter;
import com.wwwarehouse.resource_center.bean.rules.TaskUnitBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Joseph on 2017/8/25.
 *         <p/>
 */

public class SelectTaskChildFragment extends TheParentFragment {

    private RecyclerView mRvContent ;
     SelectObjAdapter mAdapter;
    private int pos;
    private int num;
    public static final int GET_DISTRIBUT_ABLE_UNITS = 1;  //获取可分配的任务
    protected ArrayList<TaskUnitBean.DataBean.ListBean> mSelectTasksListBean;//任务
    public ArrayList<TaskUnitBean.DataBean.ListBean> tagBeanList = new ArrayList<>();
    @Override
    protected int getContentId() {
        return R.layout.fragment_rule_select_application_objectst;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
    @Override
    public void onReLoad(int code) {

    }

    @Override
    protected void init(View view) {
        mRvContent =  findView(view, R.id.rv_content);
    }
    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        pos = b.getInt("pos");
        num = b.getInt("num");
        mSelectTasksListBean = b.getParcelableArrayList("mSelectTasksListBean");
        Map<String, Object> map = new HashMap<>();
        map.put("page",pos);
        map.put("size",num);
        map.put("sort","UPDATE_TIME DESC");
        httpPost(ResourceConstant.GET_DISTRIBUT_ABLE_UNITS,map,GET_DISTRIBUT_ABLE_UNITS,true,"");

//        mRvContent.addItemDecoration(new SpaceItemDecoration(10));
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity,3){
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }
            @Override
            public boolean canScrollVertically() {
                return false;
            }

        });
    }

    @Override
    public void onSuccess(CommonClass commonClass, int i) {
        switch (i) {
            case GET_DISTRIBUT_ABLE_UNITS:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    TaskUnitBean.DataBean dataBean = JSON.parseObject(commonClass.getData().toString(), TaskUnitBean.DataBean.class);
                    tagBeanList = (ArrayList<TaskUnitBean.DataBean.ListBean>) dataBean.getList();
                    if (null!= tagBeanList&&tagBeanList.size()>0&&mSelectTasksListBean!=null&&mSelectTasksListBean.size()>0){
                        for (int i1 = 0; i1 < tagBeanList.size(); i1++) {
                            for (int i2 = 0; i2 < mSelectTasksListBean.size(); i2++) {
                                if (tagBeanList.get(i1).getAcitonUnitType().equals(mSelectTasksListBean.get(i2).getAcitonUnitType())){
                                    tagBeanList.get(i1).setSelect(true);
                                }
                            }
                        }
                    }

                    final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            observer.removeGlobalOnLayoutListener(this);
                            mAdapter = new SelectObjAdapter(mActivity, tagBeanList, mRvContent.getHeight(),mRvContent.getWidth());
                            mRvContent.setAdapter(mAdapter);
                        }
                    });
                }
                break;
        }
    }

    public class SpaceItemDecoration extends RecyclerView.ItemDecoration{

        private int Space;

        public SpaceItemDecoration(int space) {
            this.Space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left = Space;
            outRect.bottom = Space;
            outRect.top = Space;
        }
    }
}
